import axios from "axios";
import router from "@/router"

const req = axios.create({
    baseURL: 'http://localhost:8080',
    timeout: 30000
})

req.interceptors.request.use(config => {
    // config.headers['content-typeafgs'] = "agaesdg" //设置请求头
    config.headers['content-type'] = 'application/json;charset=utf-8' //不显示在请求头中
    let user = JSON.parse(localStorage.getItem("honey-user") || '{}')
    config.headers['token'] = user.token

    return config
}, error => {
    console.error('request error:' + error)
    return Promise.reject(error)
})

req.interceptors.response.use(
    response => {
        let res = response.data

        if(typeof res === 'string') {
            res = res ? JSON.parse(res) : res
        }
        if(res.code === '401') {
            router.push('/Login')
        }
        return res
    }, error => {
        console.error('response error:' + error)
        return Promise.reject(error)
    }
)
export default req